//
// daytime_client.cpp
// ~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <iostream>
#include <string>
#include <vector>

#include <boost/asio.hpp>
#include "database.pb.h"
using boost::asio::ip::tcp;
using namespace google::protobuf;
using namespace std;

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: daytime_client <host>" << std::endl;
      return 1;
    }

    // roster
    vector<db::Database_Player*> roster;

    db::Database_Player *player;
    player = new db::Database_Player();
    player->set_name("Alex Rodriguez");
    player->set_batting_avg(0.300);
    roster.push_back(player);

    player = new db::Database_Player();
    player->set_name("Hideki Matsui");
    player->set_batting_avg(0.400);
    roster.push_back(player);

    // league
    vector<db::Database_Team*> league;
    db::Database_Team *team;

    team = new db::Database_Team();
    team->set_name("Red Sox");
    team->set_city("Boston, MA");
    league.push_back(team);

    team = new db::Database_Team();
    team->set_name("Yankees");
    team->set_city("New York, NY");
    league.push_back(team);

    db::Database *msg = new db::Database();

    msg->set_write(true);
    msg->set_query("");

    vector<db::Database_Player*>::iterator p;
    for (p = roster.begin(); p != roster.end(); p++) {
        msg->add_roster()->CopyFrom(**p);
    }

    vector<db::Database_Team*>::iterator t;
    for (t = league.begin(); t != league.end(); t++) {
        msg->add_league()->CopyFrom(**t);
    }

    //tcp::iostream s(argv[1], "daytime");
    tcp::iostream s(argv[1], "daytime");
    msg->SerializeToOstream(&s);
    s.close();
  }
  catch (std::exception& e)
  {
    std::cout << "Exception: " << e.what() << std::endl;
  }

  return 0;
}
